<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom,
    ChatDotRound,
    Filter,
    MagicStick
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { ref } from 'vue'

//获得机甲分类

const mechaSort = ref('')
import { mechaSortService } from '@/api/mecha.js'
const getMechaSort = async () => {
    let result = await mechaSortService()
    mechaSort.value = result.data
    // console.log(mechaSort.value);
}
getMechaSort()

//获取用户信息
import { userInfoService } from '@/api/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore()

const userInfoStore = useUserInfoStore()
const getUserInfo = async () => {
    let result = await userInfoService()
    //用户信息存入pinia
    userInfoStore.setInfo(result.data)
}
getUserInfo()


//头部区域条目被点击
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
const router = useRouter()
const handleCommand = (command) => {
    //判断指令
    if (command == 'logout') {
        //退出登录
        ElMessageBox.confirm(
            '你确定要退出吗?',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {

                //1.清空pinia
                tokenStore.removeToken()
                userInfoStore.removeInfo()
                //2.跳转到登录页面
                router.push('/login')

                ElMessage({
                    type: 'success',
                    message: '退出登录成功',
                })

            })
            .catch(() => {
                ElMessage({
                    type: 'info',
                    message: '用户取消了退出登录',
                })
            })
    } else {
        router.push('/user/' + command)
    }
}
</script>

<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu active-text-color="#ffd04b" background-color="#453574" text-color="#fff" router>

                <el-menu-item index="/home">
                    <span>主页面</span>
                </el-menu-item>

                <el-sub-menu index="1">
                    <template #title>
                        <!-- <el-icon>
                            <UserFilled />
                        </el-icon> -->
                        <span>机甲</span>
                    </template>
                    <el-menu-item index="/mecha">
                        <span>全部</span>
                    </el-menu-item>
                    <el-menu-item v-for="m in mechaSort" :key="m.sortId" :index="'/mecha/' + m.sortId">
                        <span>{{ m.sortName }}</span>
                    </el-menu-item>
                </el-sub-menu>

                <el-menu-item index="/driver">
                    <el-icon>
                        <User />
                    </el-icon>
                    <span>驾驶员</span>
                </el-menu-item>

                <el-menu-item index="/bc">
                    <span>背景设定</span>
                </el-menu-item>

            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>驾驶员：<strong>{{ userInfoStore.info.username }}</strong></div>
                <!-- command:条目被点击触发 -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.userPic ? userInfoStore.info.userPic : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="update" :icon="User">修改资料</el-dropdown-item>
                            <el-dropdown-item command="edition" :icon="Filter">版本信息</el-dropdown-item>
                            <el-dropdown-item command="author" :icon="ChatDotRound">关于作者</el-dropdown-item>
                            <el-dropdown-item command="thank" :icon="MagicStick">鸣谢</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <!-- <el-footer>SMC_WIKI ©2024 Created by 肆方</el-footer> -->
            <el-footer><a href="http://beian.miit.gov.cn/%22"
                    style="text-decoration: none;color: #666;">闽ICP备2024065968号</a></el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;


    .el-aside {
        background-color: #453574;
        // background-color: #8F82BC;

        &__logo {
            height: 120px;
            background: url('@/assets/smc_logo.png') no-repeat center / 180px 50px;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;

    }


}
</style>